<div class="navbar-inner">
    <div class="container-fluid">
        <ul class="nav pull-right">
            <!-- BEGIN TODO DROPDOWN -->
            <!-- END TODO DROPDOWN -->
            <li class="dropdown user">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                    <img id="avatar" class="user-avatar" alt="" src="/graduateinfo/media/image/avatar1_small.jpg" />
                    <span class="username">Admin</span>
                    <i class="icon-angle-down"></i>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="../login/login.html"><i class="icon-key"></i>退出</a></li>
                </ul>
            </li>
            <!-- END USER LOGIN DROPDOWN -->
        </ul>
    </div>
</div>
<style>
    .user-avatar {
        width: 30px;
        height: 30px;
        border-radius: 50%;
    }
</style>
<script src="/graduateinfo/media/js/cookie.js"></script>
<script src="/graduateinfo/media/api/student.js"></script>
<script src="/graduateinfo/media/js/axios.min.js" type="text/javascript"></script>
<script>
    const userInfo = getCookie('userInfo')
    if(userInfo){
        const user = JSON.parse(userInfo)
        $('.username').text(user.name)
        StudentApi.getUserImage(user.uid).then(res=>{
            $('#avatar').attr('src',res)
        }).catch(err=>{
            console.log(err)
        })
    } else {
        const uid = getCookie('uid')
        if(uid){
            StudentApi.getUserInfo(uid).then(res=>{
                if (res.data.statusCode === 200){
                    const userInfo = res.data.data
                    setCookie('userInfo', JSON.stringify(userInfo), 7)
                    $('.username').text(userInfo.name)
                    return userInfo
                }
            }).catch(err=>{
                console.log(err)
            })
            StudentApi.getUserImage(uid).then(res=>{
                $('#avatar').attr('src',res)
            }).catch(err=>{
                console.log(err)
            })
        } else {
            window.location.href = `http://${window.location.host}/graduateinfo`
        }
    }
</script>